<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);

class Login extends Super_admin {

    function __construct()
    {
            parent::__construct();

            $this->load->model(array(
                'User_model'
            ));

            $this->load->helper(array(
                    'date',
                    'file',
                    'form',
                    'html',
                    'language',
            ));
    }    

    public function index()
    {
       $this->form();
    }
    
    public function form($msg = NULL)
    {
        $this->load->view('login/form', array('msg' => NULL));
    }

   
    /**
     * expired
     *
     * Session expired. Redirect to login
     *
     * @access public
     * @param void
     * @return void
     */
    function expired() {

        $this->login('Your session expired.');
    }

    /**
     * logout
     *
     * Destroy current session
     *
     * @access public
     * @param void
     * @return void
     */
    function logout()
    {
            $data = array(
                    'user_id' => '',
                    'user_no' => '',
                    'user_type' => '',
            );

            $this->session->unset_userdata($data);
            $this->session->sess_destroy();

            $this->load->view('login/form', array('msg' => 'You have successfully logged out'));
    } 
    /**
     * submit
     *
     * Validate login
     *
     * @access public
     * @param void
     * @return void
     */
    function check_login() {
        $msg = FALSE;

        $user_no = $this->input->post('user_no', TRUE);
        $password = $this->input->post('password', TRUE);

        $select = 'user_id, user_no, password, user_type';

        $user = $this->User_model->get_record(array(
            'select' => $select,
            'where' => array(
                        'user_no' => $user_no,
                        'password' => md5($password),
                        'status' => 'Active'
                        )
        ));

        if (is_array($user)) {
            $msg = TRUE;
            //set a userdata (username) if login is correct
            $sess_data = array(
                'user_no' => $user['user_no'],
                'user_id' => $user['user_id'],
                'user_type' => $user['user_type']
            );

            $user_info = $this->session->set_userdata($sess_data);
        }

        echo json_encode($msg);
    }

}

/* End of file login.php */
/* Location: ./application/controllers/login.php */